<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ include file="/example/main/templates/templateConstants.jsp"%>
<%@include file="/example/main/templates/commonDWR.jspf"%>	
<%@include file="/agi/templates/templateCommon.jspf"%>			

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>KK : Auto Leasing</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<body>



<center>
<div class="topic">
<div class="topicBullet"></div>
<div class="topicText">Database Browser</div>
<div class="topicRight"></div>
</div>
<br />
<div id="dialoginfo"></div>
<s:form action="dbbrowser.jsp" method="POST">
<s:token />
<s:hidden id="selectedId" name="selectedId" />
<div class="tableContent">
  <table>
    <tr>
      <td class="left">Driver name</td>
      <td><s:textfield id="driver" name="driver" cssClass="textboxchar"/></td>
    </tr>
    <tr>
      <td class="left">URL</td>
      <td><s:textfield id="url" name="url"/></td>
    </tr>
    <tr>
      <td class="left">Username</td>
      <td><s:textfield id="user" name="user"/></td>
    </tr>
    <tr>
      <td class="left">Password</td>
      <td>
      <s:textfield id="password" name="password"/></td>
    </tr>
     <tr>
     <td class="left">SQL Command</td>
      <td>
      <s:textarea id="sql" name="sql" cols="50" rows="5"/></td>
     </tr> 
      <tr><td></td>
      <td>
     <s:submit label="submit"></s:submit>
     </td>
     </tr> 
  </table>
</div>
</s:form>

<br />
<br />

  <%
    
    String driverClass        = request.getParameter("driver");
    String connectionURL  = request.getParameter("url");
    String userID             = request.getParameter("user");
    String userPassword       = request.getParameter("password");
    
    String sql = request.getParameter("sql");
   
    
    if( sql != null && sql.length() > 0 ){
		Class.forName(driverClass);
		Connection c = DriverManager.getConnection(connectionURL, userID, userPassword);
		Statement s = c.createStatement();
		
		if( sql.trim().startsWith("SELECT") || sql.trim().startsWith("Select") || sql.trim().startsWith("select") ){
			out.print("<table>");
			sql =  sql.toUpperCase();
			ResultSet r = s.executeQuery(sql);
			int columnCount = r.getMetaData().getColumnCount();		
			ResultSetMetaData metaData = r.getMetaData();
			out.print("<tr>");
			for( int i = 0; i < columnCount; i++ ){
				out.print("<td>");
				out.print(metaData.getColumnName(i+1));
				out.print("<td>");
			}
			out.print("</tr>");
			while (r.next()){
				out.print("<tr>");
				for( int i = 0; i < columnCount; i++ ){
					out.print("<td>");
					out.print(r.getString(i+1));
					out.print("<td>");
				}
				out.print("</tr>");
			}
			out.print("</table>");
		}
		else{
			s.executeUpdate(sql);
		}
		
		s.close();
		c.close();
    }
    
    %>



</center>
</body>
</html>
<script>
$j(document).ready(function() { 
	if('<%=request.getParameter("driver")%>'!=null)
		$j("#driver").val('<%=request.getParameter("driver")%>')
	if('<%=request.getParameter("url")%>'!=null)
		$j("#url").val('<%=request.getParameter("url")%>')
	if('<%=request.getParameter("user")%>'!=null)
		$j("#user").val('<%=request.getParameter("user")%>')
	if('<%=request.getParameter("password")%>'!=null)
		$j("#password").val('<%=request.getParameter("password")%>')
	
});
</script>
